package cn.yunhe.listener;

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * Created by zs on 2018/1/3.
 */
public class RedisMessageListener implements MessageListener {

    private RedisTemplate redisTemplate;

    @Override
    public void onMessage(Message message, byte[] bytes) {
        //获取消息
        try {
            byte[] body = message.getBody();
            //使用值序列化器转换
            String msgBody = (String) redisTemplate.getValueSerializer().deserialize(body);
            System.err.println(msgBody);
            //获取channel
            byte[] channel = message.getChannel();
            //使用字符串序列化转换器转换
            String channelStr = (String) getRedisTemplate().getStringSerializer().deserialize(channel);
            System.err.println(channelStr);
            //渠道名称转换
            String bytesStr = new String(bytes);
            System.err.println(bytesStr);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public RedisTemplate getRedisTemplate() {
        return redisTemplate;
    }
}
